package pe.edu.unmsm.pacis.shca.util;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import pe.edu.unmsm.pacis.shca.domain.exception.DomainException;

public class UtilWeb {
	
	public static  String exceptionFromProperties(DomainException ex){
		if(ex.getMessage()==null)
    		return ex.getMessage();
    	String strError = UtilWeb.getResourceBundle().getString(ex.getMessageProp());
    	if(strError==null)
    		return ex.getMessage();
    	else
    		return strError;
    }
	
	private static ResourceBundle getResourceBundle() {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = ResourceBundle.getBundle("pe.edu.unmsm.pacis.shca.resource.messages", context.getViewRoot().getLocale());
        return bundle;
    }
	
	public static void changeLanguage(String language){
		Locale locale = new Locale(language);
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
	}
	
}
